﻿using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEditor.PackageManager;
using UnityEditor.PackageManager.Requests;
using UnityEngine;

namespace LZX.Installer
{
    public class InstallerController
    {
        private static AddRequest addRequest;
        public static void InstallPackages()
        {
            InstallPackage("https://gitee.com/focus-creative-games/hybridclr_unity.git","com.code-philosophy.hybridclr");
            InstallPackage("https://gitee.com/Choppyy/UniTask.git?path=src/UniTask/Assets/Plugins/UniTask","com.cysharp.unitask");
            InstallPackage("https://gitee.com/xuan020813/lzxhotupdate.git?path=Main","lzx.hotupdate.main");
            
        }
        public static void InstallPackage(string gitUrl,string packageName)
        {
            var list = Client.List();
            while (true)
            {
                if (list.IsCompleted)
                {
                    Debug.Log(list.Error);
                    break;
                }
            }
            if (list.Status == StatusCode.Success)
            {
                foreach (var package in list.Result)
                {
                    if (package.name ==  packageName)
                    {
                        Debug.Log($"包{packageName}已安装，无需重复安装");
                        return;
                    }
                }
            }
            Debug.Log($"开始安装包：{gitUrl}");
            addRequest = Client.Add(gitUrl);
            while (true)
            {
                if(addRequest.IsCompleted)
                    break;
            }
        }

        public static void RemovePackage(string packageName)
        {
            var list = Client.List();
            while (true)
            {
                if (list.IsCompleted)
                {
                    Debug.Log(list.Error);
                    break;
                }
            }
            if (list.Status == StatusCode.Success)
            {
                foreach (var package in list.Result)
                {
                    if (package.name == packageName)
                    {
                        Debug.Log($"开始卸载包：{packageName}");
                        var removeRequest = Client.Remove(packageName);
                        while (true)
                        {
                            if (removeRequest.IsCompleted)
                                break;
                        }
                        Debug.Log($"包{packageName}卸载成功");
                        return;
                    }
                }
            }
        }

        public static string GetHyBridCLRLoaclVersion()
        {
            string str = Application.dataPath.Replace("Assets", $"/HybridCLRData/LocalIl2CppData-{Application.platform}/il2cpp/libil2cpp/hybridclr/generated/libil2cpp-version.txt");
            return File.ReadAllText(str, Encoding.UTF8);
        }
        public static bool HyBridCLRHasInstalled()
        {
            return Directory.Exists(Application.dataPath.Replace("Assets", $"HybridCLRData/LocalIl2CppData-{Application.platform}/il2cpp/libil2cpp/hybridclr"));
        }
        public static bool LZXHasInited()
        {
            return File.Exists(Application.dataPath.Replace("Assets", "Assets/LZX/Editor/ScriptableObject/Setting.asset"));
        }
        public static bool LZXHasInstalled()
        {
            return InstallerWindow.GetPackageInstallerWithmainfest("lzx.hotupdate.main",out var v);
        }
    }
}